/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */

#include "ClientUtil.h"

#include "PQException.h"
#include "OgreRoot.h"

namespace PQClient
{
	CEGUI::MouseButton MouseButtonConverter::toCEGUI(OIS::MouseButtonID id)
	{
		switch(id){
			case OIS::MB_Left:
				return CEGUI::LeftButton;
			case OIS::MB_Middle:
				return CEGUI::MiddleButton;
			case OIS::MB_Right:
				return CEGUI::RightButton;
			default:
				PQAssert("Shouldn't be here : toCEGUI(OIS::MouseButtonID)");
				return CEGUI::NoButton;
		}
	}

	OIS::MouseButtonID MouseButtonConverter::toOIS(CEGUI::MouseButton id)
	{
		switch(id){
			case  CEGUI::LeftButton:
				return OIS::MB_Left;
			case CEGUI::MiddleButton:
				return OIS::MB_Middle;
			case CEGUI::RightButton:
				return OIS::MB_Right;
			default:
				PQAssert("Shouldn't be here : toCEGUI(OIS::MouseButtonID)");
				return OIS::MB_Left;
		}

	}

	std::string& StringConverter::toStd(const CEGUI::String& str)
	{
		char sdes[2048];
		static std::string result;
		int len = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);
		wchar_t* wcstr = new wchar_t[len];
		MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, wcstr, len);

		int wlen = WideCharToMultiByte(CP_ACP, 0, wcstr, -1, NULL, 0, NULL, NULL);
		char* nstr = new char[wlen];
		WideCharToMultiByte(CP_ACP, 0, wcstr, -1, nstr, wlen, NULL, NULL);

		strcpy(sdes, nstr);

		delete[] wcstr;
		delete[] nstr;

		result = sdes;

		return result;
	}


	VideoMode RenderSystemConfig::getVideoMode()
	{
		Ogre::ConfigOptionMap cfg=Ogre::Root::getSingletonPtr()->getRenderSystem()->getConfigOptions();
		Ogre::ConfigOptionMap::iterator it=cfg.find("Video Mode");

		if(it!=cfg.end()){
			Ogre::String val=it->second.currentValue;
			if(val=="800 x 600 @ 32-bit colour"){
				return VideoMode_800_600_32;
			}

			if(val=="800 x 600 @ 16-bit colour"){
				return VideoMode_800_600_16;
			}

			if(val=="1024 x 768 @ 32-bit colour"){
				return VideoMode_1024_768_32;
			}

			if(val=="1024 x 768 @ 16-bit colour"){
				return VideoMode_1024_768_16;
			}

			if(val=="800 x 600 @ 32-bit colour"){
				return VideoMode_800_600_32;
			}
		}

		return VideoMode_1024_768_32;
	}

	int RenderSystemConfig::getRenderWidth()
	{
		VideoMode mode=getVideoMode();
		switch(mode){
			case VideoMode_800_600_32:
			case VideoMode_800_600_16:

				return 800;
			case VideoMode_1024_768_32:
			case VideoMode_1024_768_16:
				
				return 1024;
		}
		return 1024;
	}

	int RenderSystemConfig::getRenderHeigh()
	{
		VideoMode mode=getVideoMode();
		switch(mode){
			case VideoMode_800_600_32:
			case VideoMode_800_600_16:

				return 600;
			case VideoMode_1024_768_32:
			case VideoMode_1024_768_16:
				
				return 768;
		}

		return 768;
	}


}
